package com.example.personnel_management_system.controller;

import com.example.personnel_management_system.entity.Salary;
import com.example.personnel_management_system.service.ISalaryService;
import com.example.personnel_management_system.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("salary")
public class SalaryController {
    @Autowired
    private ISalaryService salaryService;
    @RequestMapping("addsalary")
    public Result<Void>addsalary(Salary salary, HttpSession session){
        try {
            Integer uID=(Integer) session.getAttribute("UId");
            salaryService.insert(salary,uID);
            return Result.success();
        }catch (Exception e){
            return Result.failure();
        }
    }
    @RequestMapping("findBysStatus")
    public Result<List<Salary>>findBysStatus(Integer sStatus){
        try {
            return Result.success(salaryService.findBysStatus(sStatus));
        }catch (Exception e){
            return Result.failure();
        }
    }
    @RequestMapping("findBysID")
    public Result<Salary>findBysID(Integer sID){
        try {
            return Result.success(salaryService.findBysID(sID));
        }catch (Exception e){
            return Result.failure();
        }
    }
    @RequestMapping("delete")
    public Result<Void>delete(Integer sID){
        try {
            salaryService.delete(sID);
            return Result.success();
        }catch (Exception e){
            return Result.failure();
        }
    }
    @RequestMapping("update")
    public Result<Void>update(Salary salary,HttpSession session){
        try {
            salaryService.update(salary,(Integer) session.getAttribute("UId"));
            return Result.success();
        }catch (Exception e){
            return Result.failure();
        }
    }
}
